package com.serkol.abmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.abmanager.bean.GridResult;
import com.serkol.abmanager.constant.ResultConstant;
import com.serkol.abmanager.entity.Automobiles;
import com.serkol.abmanager.service.AutomobilesService;
import com.serkol.abmanager.mapper.AutomobilesMapper;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
public class AutomobilesServiceImpl extends ServiceImpl<AutomobilesMapper, Automobiles>
        implements AutomobilesService {
    @Override
    public GridResult findByList(int page, int pagesize, String brand, String carNum, String model, long groupId) {

        GridResult gridResult = new GridResult();
        QueryWrapper<Automobiles> queryWrapper = new QueryWrapper<>();
        if(!brand.isEmpty()){
            queryWrapper.lambda().eq(Automobiles::getBrand,brand);
        }

        if(!carNum.isEmpty()){
            queryWrapper.lambda().eq(Automobiles::getCarNum,carNum);
        }
        if(!model.isEmpty()){
            queryWrapper.lambda().eq(Automobiles::getModel,model);
        }
        if(groupId>0){
            queryWrapper.lambda().eq(Automobiles::getGroupId,groupId);
        }
        Page<Automobiles> page0 = new Page<>(page, pagesize);
        IPage<Automobiles> page1 = this.baseMapper.selectPage(page0, queryWrapper);

        if (page1.searchCount()) {
            long count = this.count(queryWrapper);
            gridResult.setPage(page1.getCurrent());
            gridResult.setTotal(count);
            int pagesize1 = Long.valueOf(count).intValue()/ pagesize;
            if (pagesize1 <= 0) {
                pagesize1 = 1;
            }
            gridResult.setPages(pagesize1);
            gridResult.setData(page1.getRecords());
            gridResult.setStatus(ResultConstant.SUCSESS);
        } else {
            gridResult.setStatus(ResultConstant.EMPTIY);
        }

        return gridResult;
    }

}




